package org.dragonnova.meetingapp.model.provider;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;

import org.dragonnova.meetingapp.model.bean.MeetingInfo;
import org.dragonnova.meetingapp.model.bean.VoiceInfo;
import org.dragonnova.meetingapp.utils.Constants;

/**
 * Created by hucn on 2016/5/11.
 * Description:存放会议数据和聊天数据
 */
public class RecordContentProvider extends ContentProvider {

    private static final UriMatcher mUriMatcher;
    private static final int MEETINGS = 1;
    private static final int MEETING = 2;
    private static final int RECORDS = 3;
    private static final int RECORD = 4;
    private static final int MULTI = 5;
    private static ContentResolver mContentResolver = null;

    static {
        mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        mUriMatcher.addURI(Constants.AUTHORITY, MeetingInfo.CONTENT, MEETINGS);
        mUriMatcher.addURI(Constants.AUTHORITY, MeetingInfo.CONTENT + "/#", MEETING);
        mUriMatcher.addURI(Constants.AUTHORITY, VoiceInfo.CONTENT, RECORDS);
        mUriMatcher.addURI(Constants.AUTHORITY, VoiceInfo.CONTENT + "/#", RECORD);
        mUriMatcher.addURI(Constants.AUTHORITY, VoiceInfo.CONTENT + "/" + MeetingInfo.CONTENT, MULTI);
    }

    private RecordDBHelper dbHelper;

    @Override
    public boolean onCreate() {
        dbHelper = new RecordDBHelper(getContext(),
                RecordDBHelper.DATABASE_NAME, null,
                RecordDBHelper.DATABASE_VERSION);
        if (getContext() != null) mContentResolver = getContext().getContentResolver();
        return true;
    }

    @Override
    public String getType(Uri uri) {
        switch (mUriMatcher.match(uri)) {
            case MEETING:
            case RECORD:
                return "vnd.android.cursor.item/" + Constants.AUTHORITY;
            case MEETINGS:
            case RECORDS:
            case MULTI:
                return "vnd.android.cursor.dir/" + Constants.AUTHORITY;
            default:
                throw new IllegalArgumentException("Unsupported uri: " + uri);
        }
    }


    @Override
    public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int updateCount;
        switch (mUriMatcher.match(uri)) {
            case MEETINGS:
            case MEETING:
                updateCount = db.update(MeetingInfo.CONTENT, values, selection, selectionArgs);
                break;
            case RECORDS:
            case RECORD:
                updateCount = db.update(VoiceInfo.CONTENT, values, selection, selectionArgs);
                break;
            case MULTI:
                updateCount = 0;
                break;
            default:
                throw new IllegalArgumentException("Failed to updateMeeting with wrong uri:" + uri);
        }
        return updateCount;
    }

    @Override
    public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int deleteCount;
        switch (mUriMatcher.match(uri)) {
            case MEETING:
            case MEETINGS:
                deleteCount = db.delete(MeetingInfo.CONTENT, selection, selectionArgs);
                break;
            case RECORD:
            case RECORDS:
                deleteCount = db.delete(VoiceInfo.CONTENT, selection, selectionArgs);
                break;
            case MULTI:
                deleteCount = 0;
                break;
            default:
                throw new IllegalArgumentException("Failed to delete with wrong uri:" + uri);
        }
        mContentResolver.notifyChange(uri, null);
        return deleteCount;
    }

    @Override
    public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
            throws NullPointerException, IllegalArgumentException {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor;
        switch (mUriMatcher.match(uri)) {
            case MEETING:
            case MEETINGS:
                cursor = db.query(MeetingInfo.CONTENT, projection, selection, selectionArgs, null, null, sortOrder);
                break;
            case RECORD:
            case RECORDS:
                cursor = db.query(VoiceInfo.CONTENT, projection, selection, selectionArgs, null, null, sortOrder);
                break;
            case MULTI:
                String table1 = uri.getPathSegments().get(0);
                String table2 = uri.getPathSegments().get(1);
                if (table1.equalsIgnoreCase(VoiceInfo.CONTENT) && table2.equals(MeetingInfo.CONTENT)) {
                    cursor = db.query(VoiceInfo.CONTENT, projection, selection, selectionArgs, null, null, sortOrder);
                } else {
                    throw new IllegalArgumentException("Failed to query with wrong uri:" + uri);
                }
                break;
            default:
                throw new IllegalArgumentException("Failed to query with wrong uri:" + uri);
        }
        return cursor;
    }

    @Override
    public Uri insert(@NonNull Uri uri, ContentValues values) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        long rowId;
        switch (mUriMatcher.match(uri)) {
            case MEETING:
            case MEETINGS:
                rowId = db.insert(MeetingInfo.CONTENT, "", values);
                break;
            case RECORD:
            case RECORDS:
                rowId = db.insert(VoiceInfo.CONTENT, "", values);
                break;
            case MULTI:
                rowId = 0;
                break;
            default:
                throw new IllegalArgumentException("Failed to insert with wrong uri:" + uri);
        }
        return ContentUris.withAppendedId(VoiceInfo.CONTENT_VOICE_URI, rowId);
    }
}

